package com.fakeDYChat.common.common.exception;

import cn.hutool.http.ContentType;
import com.fakeDYChat.common.common.domain.vo.resp.ApiResult;
import com.fakeDYChat.common.common.utils.JsonUtils;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@AllArgsConstructor
public enum HttpErrorEnum implements ErrorEnum {
    //未登录 401
    ACCESS_DENIED(HttpServletResponse.SC_UNAUTHORIZED, "登录失效，请重新登录");
    private Integer httpCode;
    private String msg;

    public void sendHttpError(HttpServletResponse response) throws IOException {
        response.setStatus(httpCode);
        ApiResult<Object> result = ApiResult.fail(this);
        response.setContentType(ContentType.JSON.toString(StandardCharsets.UTF_8));
        response.getWriter().write(JsonUtils.toStr(result));
    }

    @Override
    public Integer getErrorCode() {
        return httpCode;
    }

    @Override
    public String getErrorMsg() {
        return msg;
    }
}
